/**
 * ColourMap.java
 * colourmap
 * Copyright (C) 2010 Alan W.F. Boyd
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact the author via the google code web page for the project
 * <http://colourmap.googlecode.com>
 */
package com.googlecode.colourmap;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 * ColourMap is the main class for the colourMap application.
 * Given a CSV file of <x coordinate>,<y coordinate>,<data value> it produces a colour image.
 * Colours represent particular data values and points between the supplied coordinates are
 * interpolated based on surrounding points.
 *
 * @author Alan W.F. Boyd
 */
public class ColourMap
{
	/** The data file containing the coordinates and data values */
	private static String fileName = null;
	
	/** The size, in pixels, of each blob in the graph */
	private static int resolution = 5;
	
	/** The scale to use for the resulting map */
	private static float scale = 1;
	
	/**
	 * The main method that starts the program
	 * @param args the arguments to run the program with
	 */
	public static void main(String[] args)
	{
		if (args.length > 0)
			readArguments(args);
		else
			gatherArguments();
		
		GraphData mapData = new GraphData();
		mapData.loadData(fileName);
		mapData.drawMap(resolution, scale);
	}
	
	/**
	 * Parse the arguments that are passed to the program
	 * @param args the program arguments
	 */
	private static void readArguments(String[] args)
	{
		String thisArg, var, val;
		for (int i = 0; i < args.length; i++)
		{
			thisArg = args[i];
			
			var = thisArg.split("=")[0];
			val = thisArg.split("=")[1];
			
			if (var.equals("file"))
				fileName = val;
			else if (var.equals("resolution"))
			{
				try
				{
					resolution = Integer.parseInt(val);
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "The resolution '" + val + "' is invalid\nResolution should be an integer.", "Invalid Resolution", JOptionPane.ERROR_MESSAGE);
					System.exit(-1);
				}
				
				if (resolution < 1)
				{
					JOptionPane.showMessageDialog(null, "The resolution '" + val + "' is invalid\nResolution should be greater than 1.", "Invalid Resolution", JOptionPane.ERROR_MESSAGE);
					System.exit(-1);
				}
			}
			else if (var.equals("scale"))
			{
				try
				{
					scale = Float.parseFloat(val);
				}
				catch (NumberFormatException e)
				{
					JOptionPane.showMessageDialog(null, "The scale '" + val + "' is invalid\nScale should be a floating point number.", "Invalid Scale", JOptionPane.ERROR_MESSAGE);
					System.exit(-1);
				}
			}
		}
	}
	
	/**
	 * Gather any arguments that weren't specified on the command line
	 */
	private static void gatherArguments()
	{
		selectFile();
		selectResolution();
		selectScale();
	}

	/**
	 * Queries the user for the resolution to use in the colour map
	 */
	private static void selectResolution()
	{
		String text = JOptionPane.showInputDialog("What resolution to use?", "5");

		if (text == null)
			System.exit(0);
			
		// No whitespace
		text = text.replaceAll("\\s", "");
			
		// Convert to number
		try
		{
			resolution = Integer.parseInt(text);
		}
		catch (NumberFormatException e)
		{
			JOptionPane.showMessageDialog(null, "The resolution '" + text + "' is invalid\nResolution should be an integer.", "Invalid Resolution", JOptionPane.ERROR_MESSAGE);
			selectResolution();
		}
	}
	
	/**
	 * Queries the user for the scale to use in the colour map
	 */
	private static void selectScale()
	{
		String text = JOptionPane.showInputDialog("What scale to use?", "1");

		if (text == null)
			System.exit(0);
			
		// No whitespace
		text = text.replaceAll("\\s", "");
			
		// Convert to number
		try
		{
			scale = Float.parseFloat(text);
		}
		catch (NumberFormatException e)
		{
			JOptionPane.showMessageDialog(null, "The scale '" + text + "' is invalid\nScale should be a floating point number.", "Invalid Scale", JOptionPane.ERROR_MESSAGE);
			selectScale();
		}
	}

	/**
	 * Queries the user for the file to use to provide data for the colour map
	 */
	private static void selectFile()
	{
		JFileChooser fc = new JFileChooser();
		fc.setDialogTitle("Specify .csv File");
		
		if (fc.showOpenDialog(null) != JFileChooser.APPROVE_OPTION)
			System.exit(0);

		fileName = fc.getSelectedFile().getPath();
	}
}
